package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
       @Autowired
    private SetmealDishService setmealDishService;
    //添加套餐
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
         //保存套餐表setmeal的基本信息
        this.save(setmealDto);
         //获取页面传过来的setmealDto里的另一张表的信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> setmealDishes1 =  setmealDishes.stream().map((item)->{
            Long id = setmealDto.getId();
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        //保存setmeal_dish的信息
        setmealDishService.saveBatch(setmealDishes1);
    }

    /**
     * 删除套餐,要同时操作两张表
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
           //查询套餐状态,确定是否可删除
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
           //查询不能删除的
        int count = this.count(wrapper);
         if(count>0){
             throw new BusinessException("套餐在售卖,不能删除哦");
         }
          //如果可以删除,则进行删除操作,先删除套餐表中的数据
        this.removeByIds(ids);

         //删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getDishId,ids);
          //删除操作
         setmealDishService.remove(wrapper1);

    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto listWithDish(Long id) {
        //根据id查询套餐表setmeal
        Setmeal setmeal = this.getById(id);

        //new 一个setmealdto表,把setmeal放进去
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //根据id查询setmealdish表的数据
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改数据
     * @param setmealDto
     */
    @Override
    public void updateWith(SetmealDto setmealDto) {
        //修改套餐表setmeal的数据
        this.updateById(setmealDto);

        //获取setmealdto里的setmeal_dish的数据
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
         //遍历,操作
        List<SetmealDish> dishes1 =  dishes.stream().map((item)->{
             Long id = setmealDto.getId();
             item.setSetmealId(id);
             return item;
         }).collect(Collectors.toList());

        //保存setmeal_dish表
         setmealDishService.updateBatchById(dishes1);
    }
}
